package com.laikan.reader.bookreader.engines;

import android.content.Context;
import android.graphics.*;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.core.utils.PublicUtil;

import java.io.File;
import java.io.InputStream;

/**
 * 页面布局相关基础类
 *
 * @author
 */
public class PageStyleSet {

    /**
     * 屏幕密度 与字体大小转换系数!!!存储的一定为转换后的结果
     */
    public static float density = 1;
    public static float scaledDensity;
    /**
     * 屏幕宽度
     */
    public static int screenWidth;
    /**
     * 屏幕高度
     */
    public static int screenHeight;
    private volatile static PageStyleSet instance = null;
    // 正文图片布局
    public int imageWidth = 200;
    public int imageHeight = 200;
    public int imagePadding = 0;// 默认居中对齐,0居中对齐，-1靠左，1靠右
    /**
     * 与上边缘的距离
     */
    public int marginTop = screenWidth / 16;
    /**
     * 与下边缘的距离      分辨率 * 0.0704
     */
    public int marginBottom = 45;
    /**
     * 与水平边缘的距离
     */
    public int marginHorizontal = (int) (screenWidth * 0.0468);
    /**
     * 页码与右边的距离
     */
    public int marginBottomRigth = 90;
    /**
     * 行间距与字体 比例系数
     */
    public float lineSpaceingRatio = 0.382f;
    /**
     * 段间距与字体 比例系数
     */
    public float paragraphSpacingRatio = 0.5f;
    /**
     * 段首行缩进距离,通常为两个汉字 比例系数
     */
    public float paragraphIndentRatio = 2.0f;
    /**
     * 字间距
     */
    public float wordSpacingRatio = 0.00f;
    /**
     * 屏幕亮度
     */
    public int brightness = 50;
    /**
     * 屏幕亮度是否跟随系统
     */
    public Boolean isAutoBrightness = false;
    /**
     * 是否是夜间模式 true 夜间模式 false 日间模式
     */
    public Boolean isNighttime = false;
    /**
     * 背景图片，可以是纯色的背景，也可以是带图片的背景。背景图为空时使用纯色
     */
    public Bitmap bg = null;
    /**
     * 背景图片，可以是纯色的背景，也可以是带图片的背景。背景图为空时使用纯色
     */
    public int bgColor = 0;
    /**
     * 尺寸大小或者像素是否跟随系统有关
     */
    public Boolean isDeviceDependent = false;
    /**
     * 是否是 全屏阅读
     */
    public Boolean isFullScreen = true;
    /**
     * 是否是 显示章节名称
     */
    public Boolean isShowChapterName = true;
    /**
     * 是否是 显示阅读状态栏
     */
    public Boolean isShowState = false;
    /**
     * 是否是 启用快捷键操作
     */
    public Boolean isUseDF = true;
    /**
     * 是否是 显示封面页
     */
    public Boolean isShowBookTop = true;
    /**
     * 是否是 显示封底页
     */
    public Boolean isShowBookBottom = true;
    /**
     * 是否是 互动区域
     */
    public Boolean isShowActionRect = true;
    /**
     * 是否是 作者说
     */
    public Boolean isShowBookAuthor = true;
    /**
     * 读书休息提醒，关闭为0
     */
    public int ReadBreak = 45;
    /**
     * 锁屏时间，从不锁屏为0
     */
    public int LockScreenTime = 0;
    /**
     * 状态栏高度
     */
    public int StatusHeight;
    /**
     * 标题距顶部的距离，像素单位
     */
    public int distanceTop = 50;
    /**
     * 作者说距顶部的距离，像素单位
     */
    public int zzsDistanceTop = 30;

    RePageInter rePageInter;
    Paint contentPaint = null;
    Paint statePaint = null;
    private  Bitmap bgcacheH = null;

    private  Bitmap bgcache01 = null;
    private  Bitmap bgcache02 = null;
    private  Bitmap bgcache03 = null;
    private  Bitmap bgcache04 = null;
    private  Bitmap bgcache05 = null;

    /**
     * 标题字体
     */
    private Paint titlePaint;
    private Paint linePaint;

    public int textSize;
    public int NightColor = 0XFFCCCCCC;
    public int DayColor = 0xFF333333;

    public static PageStyleSet Instance() {
        if(instance == null) {
            synchronized(PageStyleSet.class) {
                if(instance == null) {
                    instance = new PageStyleSet();
                }
            }
        }
        return instance;
    }



    private Context context;
    private InputStream night;
    public void Init(Context context) {
        this.context = context;

        night = context.getResources().openRawResource(+R.drawable.lk_reader_bg6);
        bgcache01 = PublicUtil.resizeBitmap(BitmapFactory.decodeStream(context.getResources().openRawResource(+R.drawable.readerdetail_bg01)), screenWidth, screenHeight);
        bgcache02 = PublicUtil.resizeBitmap(BitmapFactory.decodeStream(context.getResources().openRawResource(+R.drawable.readerdetail_bg02)), screenWidth, screenHeight);
        bgcache03 = PublicUtil.resizeBitmap(BitmapFactory.decodeStream(context.getResources().openRawResource(+R.drawable.readerdetail_bg03)), screenWidth, screenHeight);
        bgcache04 = PublicUtil.resizeBitmap(BitmapFactory.decodeStream(context.getResources().openRawResource(+R.drawable.readerdetail_bg04)), screenWidth, screenHeight);
        bgcache05 = PublicUtil.resizeBitmap(BitmapFactory.decodeStream(context.getResources().openRawResource(+R.drawable.readerdetail_bg05)), screenWidth, screenHeight);

        bgcacheH = PublicUtil.resizeBitmap(BitmapFactory.decodeStream(night), screenWidth, screenHeight);
        DeviceIndependent();
        marginTop = (int) (screenHeight * (80f / 1136f));
        marginHorizontal = (int) (screenWidth * (30f / 640f));
        int fontSize = SPUtil.getInt("fontSize", 18);
        textSize = (int) (fontSize * density);
        initlinSpace(SPUtil.getInt("content_interval", 2));
        setTextSize(textSize);
        setFontType();
        setPaintColor(SPUtil.getInt("bg_model",0),false);

    }

    public void setFontType(){

        String fontTypePath = "/LaikanReader/font/";
        File filePath = PublicUtil.getRootPath();
        File file = null;
        int typeFont = SPUtil.getInt("typeface_check",1);
        switch (typeFont){
            case 1:
                GetContentPaint().setTypeface(null);
                GetTitlePaint().setTypeface(null);
                break;
            case 2:
                file = new File(filePath.getPath() + fontTypePath + "FZXKT.ttf");
                setPaint(file);

                break;
            case 3:
                file = new File(filePath.getPath() + fontTypePath + "FZLTHJW.ttf");
                setPaint(file);

                break;
            case 4:
                file = new File(filePath.getPath() + fontTypePath + "FZXY.ttf");
                setPaint(file);

                break;

        }
    }

    /**
     * 设置画笔
     * @param file
     */
    private void setPaint(File file){
        Typeface tf;
        if(file.exists()){
            tf = Typeface.createFromFile(file);//根据路径得到Typeface
            GetContentPaint().setTypeface(tf);
            GetTitlePaint().setTypeface(tf);
        }else{
            GetContentPaint().setTypeface(null);
            GetTitlePaint().setTypeface(null);
            SPUtil.putInt("typeface_check",1);
        }
    }

    /**
     *  初始化行距
     * @param linespace
     */
    private void initlinSpace(int linespace){

        switch (linespace){
            case 1:
                lineSpaceingRatio = 0.282f;
                break;
            case 2:
                lineSpaceingRatio = 0.382f;
                break;
            case 3:
                lineSpaceingRatio = 0.75f;
                break;
        }
    }

    /**
     * 设置字体大小
     * @param textSize
     */
    public void setTextSize(int textSize){
        this.textSize = textSize;
        GetContentPaint().setTextSize(textSize);
        GetTitlePaint().setTextSize(textSize+3*density);
    }

    /**
     * 设置行距
     * @param lineSpace
     */
    public void setLinSpace(float lineSpace){
        this.lineSpaceingRatio = lineSpace;
    }

    /**
     * 设置画笔颜色
     * @param type
     */
    public void setPaintColor(int type,boolean bo){
        isNighttime = SPUtil.getBoolean("model_flag", false);
        if (isNighttime) {
            bg = bgcacheH;
            GetTitlePaint().setColor(Color.parseColor("#FFCCCCCC"));
            GetContentPaint().setColor(Color.parseColor("#FFCCCCCC"));
            GetStatePaint().setColor(Color.parseColor("#FFCCCCCC"));
            GetLinePaint().setColor(Color.parseColor("#FFCCCCCC"));
        }else {
            switch (type){

                case 0:
                    bg = bgcache01;
                    GetTitlePaint().setColor(Color.parseColor("#FF333333"));
                    GetContentPaint().setColor(Color.parseColor("#FF333333"));
                    GetLinePaint().setColor(Color.parseColor("#FF333333"));
                    GetStatePaint().setColor(Color.parseColor("#FF999999"));
                    break;
                case 2:
                    bg = bgcache02;
                    GetTitlePaint().setColor(Color.parseColor("#FF333333"));
                    GetContentPaint().setColor(Color.parseColor("#FF333333"));
                    GetLinePaint().setColor(Color.parseColor("#FF333333"));
                    GetStatePaint().setColor(Color.parseColor("#FF999999"));
                    break;
                case 4:
                    bg = bgcache03;
                    GetTitlePaint().setColor(Color.parseColor("#FF333333"));
                    GetContentPaint().setColor(Color.parseColor("#FF333333"));
                    GetLinePaint().setColor(Color.parseColor("#FF333333"));
                    GetStatePaint().setColor(Color.parseColor("#FF999999"));
                    break;
                case 12:
                    bg = bgcache04;
                    GetTitlePaint().setColor(Color.parseColor("#FF333333"));
                    GetContentPaint().setColor(Color.parseColor("#FF333333"));
                    GetLinePaint().setColor(Color.parseColor("#FF333333"));
                    GetStatePaint().setColor(Color.parseColor("#FF999999"));
                    break;
                case 14:
                    bg = bgcache05;
                    GetTitlePaint().setColor(Color.parseColor("#FF333333"));
                    GetContentPaint().setColor(Color.parseColor("#FF333333"));
                    GetLinePaint().setColor(Color.parseColor("#FF333333"));
                    GetStatePaint().setColor(Color.parseColor("#FF999999"));
                    break;
            }
        }

        if(rePageInter!=null&&bo){
            rePageInter.repage();
        }

    }

    public interface RePageInter{
        public void repage();
    }

    public void setRePageInter(RePageInter l){
        this.rePageInter = l;
    }
    public void setLineColor(Paint p){

    }

    /**
     * 设置电池图片的颜色
     * @param mBitmap
     * @return
     */
    public  Bitmap setBatteryColor(Bitmap mBitmap) {
        Bitmap bitmap = null;
        if (isNighttime) {    // 夜间模式
            bitmap = PublicUtil.getAlphaBitmap(mBitmap, NightColor);

        }else {    // 日间模式
            int bg_model = SPUtil.getInt("bg_model", 0);
            switch (bg_model) {
                case 0:
                case 2:
                case 4:
                case 6:
                    bitmap = mBitmap;
                    break;

                case 8:
                    int color8 = context.getResources().getColor(R.color.C_1D1D1D);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color8);
                    break;

                case 10:
                    int color10 = context.getResources().getColor(R.color.C_777777);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color10);
                    break;

                case 12:
                    int color12 = context.getResources().getColor(R.color.C_EFEFEF);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color12);
                    break;

                case 14:
                    int color14 = context.getResources().getColor(R.color.C_677E94);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color14);
                    break;

                case 16:
                    int color16 = context.getResources().getColor(R.color.C_72634F);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color16);
                    break;

                case 18:
                    int color18 = context.getResources().getColor(R.color.C_86B4B2);
                    bitmap = PublicUtil.getAlphaBitmap(mBitmap, color18);
                    break;
            }
        }
        return bitmap;

    }


    /**
     * 获取线颜色
     *
     * @return
     *
     * @date 2015-11-26 下午4:36:51
     */
    public Paint GetLinePaint() {
        if(linePaint != null) {
            return linePaint;
        }
        linePaint = new Paint();
        return linePaint;
    }



    /**
     * 获取标题字体
     *
     * @return
     *
     * @date 2015-11-26 下午4:36:51
     */
    public Paint GetTitlePaint() {
        if(titlePaint != null) {
            return titlePaint;
        }
        titlePaint = new Paint();
        return titlePaint;
    }

    /**
     * 得到正文字体
     */
    public Paint GetContentPaint() {

        if(contentPaint != null) {
            return contentPaint;
        }
        contentPaint = new Paint();
        return contentPaint;
    }

    /**
     * 得到状态栏字体
     */
    public Paint GetStatePaint() {

        if(statePaint != null) {
            return statePaint;
        }
        statePaint = new Paint();
        statePaint.setTextSize(11 * density);
        return statePaint;
    }


    /**
     * 将系统尺寸大小或者像素是否跟随系统无关
     */
    public void DeviceIndependent() {
        if(isDeviceDependent) {
            return;
        }

        marginBottom = (int) (marginBottom * density);
        distanceTop = (int) (distanceTop * density);// !!!屏幕比例
        zzsDistanceTop = (int) (zzsDistanceTop * density);// !!!屏幕比例
        isDeviceDependent = true;
    }

    /**
     * 获取可视区域左右外边距的和
     */
    public int GetMarginWidth() {
        if(isDeviceDependent) {
            return 2* marginHorizontal;
        }else {
            return 2* marginHorizontal;
        }
    }

    /**
     * 获取可视区域上下外边距的和
     */
    public int GetMarginHeight() {
        if(isDeviceDependent) {
            return marginTop + marginBottom;
        }else {
            return (int) (marginTop * density + marginBottom * density);
        }
    }

}
